﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Common
{
    [Access("OP")]
    public partial class ContentPropertyManage : WebFactory.PageBase
    {
        private byte _modelId;

        protected void Page_Load(object sender, EventArgs e)
        {
            this._modelId = byte.Parse(Request["ModelId"]);
            if (!IsPostBack)
            {
                BindList(1);


                this.BC_Path.Items.Add(new ListItem {Text = ModelManage.GetModelName(this._modelId)});
                var cpo = (ContentPropertyOption) int.Parse(Request["Type"]);
                var strPath = string.Empty;
                switch (cpo)
                {
                    //1文章作者，2文章来源，3软件作者\公司，4软件作者\公司的电邮,
                    //5软件解压密码
                    case ContentPropertyOption.文章作者:
                        strPath += string.Format(" → {0}作者", ModelManage.GetContentName(this._modelId));
                        Title = string.Format("{0}作者管理", ModelManage.GetContentName(this._modelId));
                        break;
                    case ContentPropertyOption.文章来源:
                        strPath += string.Format(" → {0}来源", ModelManage.GetContentName(this._modelId));
                        Title = string.Format("{0}来源管理", ModelManage.GetContentName(this._modelId));
                        break;
                    case ContentPropertyOption.软件作者:
                        strPath += string.Format(" → {0}作者", ModelManage.GetContentName(this._modelId));
                        Title = string.Format("{0}作者", ModelManage.GetContentName(this._modelId));
                        break;
                    case ContentPropertyOption.软件作者的电邮:
                        strPath += string.Format(" → {0}作者电邮", ModelManage.GetContentName(this._modelId));
                        Title = string.Format("{0}作者电邮", ModelManage.GetContentName(this._modelId));
                        break;
                    case ContentPropertyOption.软件解压密码:
                        strPath += string.Format(" → {0}解压密码", ModelManage.GetContentName(this._modelId));
                        Title = string.Format("{0}解压密码", ModelManage.GetContentName(this._modelId));
                        break;
                }
                this.BC_Path.Items.Add(new ListItem {Text = strPath});
            }
        }


        public void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageNo = pageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.GV_List.DataSource = new BLL.ContentPropertyManage().BindList(this._modelId,
                (ContentPropertyOption) int.Parse(Request["Type"]), "", pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var model = new ContentProperty
            {
                Id = 0,
                ModelId = this._modelId,
                Name = this.txtName.Text.Trim(),
                Type = int.Parse(Request["Type"])
            };

            var resu = new BLL.ContentPropertyManage().Add(model);


            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
            else
            {
                BindList(1);
            }
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList(1);
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var strID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();
            new BLL.ContentPropertyManage().Delete(strID);
            BindList(1);
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var model = new ContentProperty
            {
                Id = (int) this.GV_List.DataKeys[e.RowIndex].Value,
                ModelId = this._modelId,
                Name = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[2].Controls[0]).Text,
                UpDateTime = DateTime.Now
            };

            var resu = new BLL.ContentPropertyManage().Add(model);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
                return;
            }

            this.GV_List.EditIndex = -1;


            BindList(this.PageNavigation1.PageNo);
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList(this.PageNavigation1.PageNo);
        }
    }
}